/*
 * Copyright @ 2025 weili23
 * com.example.ai.config 16:22
 * All right reserved.
 */

package com.example.ai.config;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2025/4/30 16:22
 * @version: v1.0
 */
@Configuration
public class ZhiyiAssistantConfig {
    @Autowired
    private MongoDbChatMemoryConfig mongoDbChatMemoryConfig;
    @Bean("chatMemoryProviderZhiyi")
    public ChatMemoryProvider chatMemoryProviderZhiyi(){
        return memoryId -> MessageWindowChatMemory
                .builder()
                .id(memoryId)
                .maxMessages(20)
                .chatMemoryStore(mongoDbChatMemoryConfig)
                .build();
    }
    //配置用内存知识库=知识库文档
    @Bean
    public ContentRetriever contentRetrieverZhiyi() {
        //使用FileSystemDocumentLoader读取指定目录下的知识库文档信息
        Document document1 = FileSystemDocumentLoader.loadDocument("D:/chatLog/医师介绍.txt");
        Document document2 = FileSystemDocumentLoader.loadDocument("D:/chatLog/科室.txt");
        Document document3 = FileSystemDocumentLoader.loadDocument("D:/chatLog/医院简介.txt");
        System.out.println(document1.text());
        System.out.println(document2.text());
        System.out.println(document3.text());
        List<Document> documents = List.of(document1, document2, document3);
        //使用内存向量存储
        InMemoryEmbeddingStore<TextSegment>  embeddingStore = new InMemoryEmbeddingStore();
        //使用默认的文档分割器
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
        //从嵌入存储中检索和查询内容
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }

}
